var config = parent.parent.CONFIG,system=parent.parent.SYSTEM;
var isRefresh = false;
var _limitMenu = system.MENULIMIT.SELL05;
var _menuParam = config.OPTIONLIMIT;
var queryurl = config.BASEPATH+'sell/card/page';

var Utils = {
	doQueryShop : function(){
		commonDia = $.dialog({
			title : '选择店铺',
			content : 'url:'+config.BASEPATH+'base/shop/to_list_sub_dialog',
			data : {multiselect:false},
			width : 450,
			height : 370,
			max : false,
			min : false,
			cache : false,
			lock: true,
			ok:function(){
				var selected = commonDia.content.doSelect();
				if (!selected) {
					return false;
				}
			},
			close:function(){
				var selected = commonDia.content.doSelect();
				if(selected){
					$("#cd_shop_code").val(selected.sp_code);
					$("#sp_name").val(selected.sp_name);
				}
			},
			cancel:true
		});
	}
};

var handle = {
	operate: function(oper, id){//修改、新增
		var height = 420;
		var width = 645;
		if(oper == 'add'){
			title = '新增';
			url = config.BASEPATH+"sell/card/to_add";
			data = {oper: oper, callback:this.callback};
		}else if(oper == 'edit'){
			height = 360;
			width = 345;
			title = '修改';
			url = config.BASEPATH+"sell/card/to_update?cd_id="+id;
			data = {oper: oper,callback: this.callback};
		}else if(oper == 'recharge'){
			height = 340;
			title = '充值';
			url = config.BASEPATH+"sell/card/to_recharge?cd_id="+id;
			data = {oper: oper,callback: this.callback};
		}else{
			return false;
		}
		$.dialog({
			title : title,
			content : 'url:'+url,
			data: data,
			width : width,
			height : height,
			max : false,
			min : false,
			cache : false,
			lock: true
		});
	},
	detail : function(id){
		var rowData = $("#grid").jqGrid("getRowData", id);
		$.dialog({
			title : '储值卡消费明细',
			content : 'url:'+config.BASEPATH+"sell/card/to_detail",
			data: {cd_code:rowData.cd_code},
			width : 895,
			height : 320,
			max : false,
			min : false,
			cache : false,
			lock: true
		});
	},
	del: function(rowId){//删除
		if(rowId != null && rowId != ''){
			var rowData = $("#grid").jqGrid("getRowData", rowId);
		 	$.dialog.confirm('数据删除后无法恢复，确定要清除吗？', function(){
				$.ajax({
					type:"POST",
					url:config.BASEPATH+'sell/card/del',
					data:{"cd_id":rowId},
					cache:false,
					dataType:"json",
					success:function(data){
						if(undefined != data && data.stat == 200){
							Public.tips({type: 3, content : '删除成功!'});
							$('#grid').jqGrid('delRowData', rowId);
						}else{
							Public.tips({type: 1, content : data.message});
						}
					}
				});
			});
	   }else{
		   Public.tips({type: 1, content : '请选择数据!'});
	   }	
    },
    updateState : function(obj,id,cd_state){
    	var rowData = $("#grid").jqGrid('getRowData', id);
    	var tip = null;
    	if(cd_state==0){
    		tip = '确认要启用储值卡吗？';
    	}else{
    		tip = '确认要停用储值卡吗？';
    	}
    	$.dialog.confirm(tip, function(){
			$.ajax({
				type:"POST",
				url:config.BASEPATH+'sell/card/updateState',
				data:{"cd_id":id,"cd_state":cd_state},
				cache:false,
				dataType:"json",
				success:function(data){
					if(undefined != data && data.stat == 200){
						$("#grid").jqGrid("setRowData", id,{cd_state:cd_state});
						if(cd_state==0){
			    			$(obj).html("&#xe60d;").removeClass("ui-icon-begin").addClass("ui-icon-stop").attr("title","停用");
			    		}else{
			    			$(obj).html("&#xe63c;").removeClass("ui-icon-stop").addClass("ui-icon-begin").attr("title","启用");
			    		}
						Public.tips({type: 3, content : "状态修改成功!"});
					}else{
						Public.tips({type: 1, content : "状态修改失败!"});
					}
				}
			});
		});
    },
    callback: function(data, oper, dialogWin){
		var gridData = $("#grid").data('gridData');
		if(!gridData){
			gridData = {};
			$("#grid").data('gridData', gridData);
		}
		gridData[data.id] = data;
		if(oper == "edit") {
			$("#grid").jqGrid('setRowData', data.id, data);
			dialogWin && dialogWin.api.close();
		}
		if(oper == "recharge") {
			$("#grid").jqGrid('setRowData', data.id, data);
			dialogWin && dialogWin.api.close();
		}
		if(oper == "add"){
			THISPAGE.reloadData();
			dialogWin && dialogWin.api.close();
		}
	},
	operFmatter : function(val, opt, row){
		var html_con = '<div class="operating" data-id="' +opt.rowId+ '">';
		if(row.cd_state == 0){
			html_con += '<i class="iconfont i-hand ui-icon-stop" title="停用">&#xe60d;</i>';
		}else{
			html_con += '<i class="iconfont i-hand ui-icon-begin" title="启用">&#xe63c;</i>';
		}
		html_con += '<i class="iconfont i-hand ui-icon-pencil" title="修改">&#xe60c;</i>';
		html_con += '<i class="iconfont i-hand ui-icon-recharge" title="充值">&#xe645;</i>';
		html_con += '<i class="iconfont i-hand ui-icon-trash" title="删除">&#xe60e;</i>';
		html_con += '<i class="iconfont i-hand ui-icon-list" title="明细">&#xe608;</i>';
		html_con += '</div>';
		return html_con;
	},
	formatTotalMoney : function(val, opt, row){
		return row.cd_money + row.cd_used_money;
	},
	formatState:function(val, opt, row){//0:正常 1:停用
		if(val == "0"){
			return "正常";
		}else if(val == "1"){
			return "停用";
		}else{
			return val;
		}
	}
};
var THISPAGE = {
	init:function (){
		this.initDom();
		this.initGrid();
		this.initEvent();	
	},
	initDom:function(){
		this.$_date = $("#date").cssRadio({ callback: function($_obj){
			dateRedioClick($_obj.find("input").attr("id"));
		}});
	},
	initGrid:function(){
		var gridWH = Public.setGrid();
		var colModel = [
	    	{name: 'operate',label:'操作',width: 120, formatter: handle.operFmatter,align:'center', sortable:false},
	    	{name: 'cd_code',label:'编号',index: 'cd_code',width:80,hidden: true},
	    	{name: 'cd_cardcode',label:'储值卡号',index: 'cd_cardcode',width:120},
	    	{name: 'cd_name',label:'姓名',index: 'cd_name',width:120},
	    	{name: 'cd_mobile',label:'手机号码',index: 'cd_mobile',width:100},
	    	{name: 'cd_idcard',label:'身份证号',index: 'cd_idcard',width:120},
	    	{name: 'shop_name',label:'发放门店',index: 'shop_name',width:100},
	    	{name: 'cd_grantdate',label:'发放日期',index: 'cd_grantdate',width:100},
	    	{name: 'cd_enddate',label:'有效日期',index: 'cd_enddate',width:100},
	    	{name: 'total_money',label:'总计金额',index: 'cd_money',width:80,align:'right',formatter: handle.formatTotalMoney},
	    	{name: 'cd_used_money',label:'已用金额',index: 'cd_used_money',width:80,align:'right'},
	    	{name: 'cd_money',label:'剩余金额',index: 'cd_money',width:80,align:'right'},
	    	{name: 'cd_realcash',label:'实收现金',index: 'cd_realcash',width:80,align:'right'},
	    	{name: 'cd_bankmoney',label:'实收刷卡',index: 'cd_bankmoney',width:80,align:'right'},
	    	{name: 'cd_state',label:'状态',index: 'cd_state',width:80,formatter:handle.formatState},
	    	{name: 'cd_cashrate',label:'现金率',index: 'cd_cashrate',width:80,align:'right'},
	    	{name: 'cd_manager',label:'经办人',index: 'cd_manager',width:100}
	    ];
		$('#grid').jqGrid({
            url:queryurl,			
			datatype: 'json',
			width: gridWH.w-12,
			height: gridWH.h-20,
			gridview: true,
			colModel: colModel,
			rownumbers: true,//行号
			pager: '#page',//分页
			pgbuttons:true,
			recordtext:'{0} - {1} 共 {2} 条',  
			multiselect:false,//多选
			viewrecords: true,
			rowNum:config.BASEROWNUM,//每页条数
			rowList:config.BASEROWLIST,//分页条数
			cmTemplate: {sortable:true,title:false},
			shrinkToFit:false,//表格是否自动填充
			jsonReader: {
				root: 'data.list',
				total: 'data.pageInfo.totalPage',
                page: 'data.pageInfo.currentPage',
                records: 'data.pageInfo.totalRow',
				repeatitems : false,
				id: 'cd_id'  //图标ID
			},
			loadComplete: function(data){
			},
			loadError: function(xhr, status, error){		
			}
	    });
	},
	buildParams : function(){
		var params = '';
		params += 'begindate='+$("#begindate").val();
		params += '&enddate='+$("#enddate").val();
		params += '&cd_shop_code='+$("#cd_shop_code").val();
		params += '&cd_cardcode='+Public.encodeURI($("#cd_cardcode").val());
		params += '&cd_name='+Public.encodeURI($("#cd_name").val());
		params += '&cd_mobile='+Public.encodeURI($("#cd_mobile").val());
		return params;
	},
	reset:function(){
		$("#cd_shop_code").val("");
		$("#cd_cardcode").val("");
		$("#cd_name").val("");
		$("#cd_mobile").val("");
		$("#sp_name").val("");
		THISPAGE.$_date.setValue(0);
		$("#begindate").val("");
		$("#enddate").val("");
	},
	reloadData:function(){
		var param=THISPAGE.buildParams();
		$("#grid").jqGrid('setGridParam',{datatype:"json",page:1,url:queryurl+'?'+param}).trigger("reloadGrid");
	},
	initEvent:function(){
		//查询
		$('#btn-search').on('click', function(e){
			e.preventDefault();
			if (!Business.verifyRight(_limitMenu,_menuParam.SELECT)) {
				return ;
			}
			THISPAGE.reloadData();
		});
		$('#btn_ok').on('click', function(e){
			e.preventDefault();
			if (!Business.verifyRight(_limitMenu,_menuParam.SELECT)) {
				return ;
			}
			Public.hideConstion();
			THISPAGE.reloadData();
		});
		//新增
		$('#btn-add').on('click', function(e){
			e.preventDefault();
			if (!Business.verifyRight(_limitMenu,_menuParam.INSERT)) {
				return ;
			}
			handle.operate('add');
		});
		//修改
		$('#grid').on('click', '.operating .ui-icon-pencil', function(e){
			e.preventDefault();
			if (!Business.verifyRight(_limitMenu,_menuParam.UPDATE)) {
				return ;
			}
			var id = $(this).parent().data('id');
			handle.operate('edit', id);
		});
		$('#grid').on('click', '.operating .ui-icon-recharge', function(e){
			e.preventDefault();
			if (!Business.verifyRight(_limitMenu,_menuParam.UPDATE)) {
				return ;
			}
			var id = $(this).parent().data('id');
			handle.operate('recharge', id);
		});
		$('#grid').on('click', '.operating .ui-icon-begin', function(e){
			e.preventDefault();
			if (!Business.verifyRight(_limitMenu,_menuParam.UPDATE)) {
				return ;
			}
			var id = $(this).parent().data('id');
			handle.updateState(this, id,0);
		});
		$('#grid').on('click', '.operating .ui-icon-stop', function(e){
			e.preventDefault();
			if (!Business.verifyRight(_limitMenu,_menuParam.UPDATE)) {
				return ;
			}
			var id = $(this).parent().data('id');
			handle.updateState(this, id,1);
		});
		//删除
		$('#grid').on('click', '.operating .ui-icon-trash', function(e){
			e.preventDefault();
			if (!Business.verifyRight(_limitMenu,_menuParam.DELETE)) {
				return ;
			}
			var id = $(this).parent().data('id');
			handle.del(id);
		});
		$('#grid').on('click', '.operating .ui-icon-list', function(e){
			e.preventDefault();
			var id = $(this).parent().data('id');
			handle.detail(id);
		});
		$('#btn_reset').on('click', function(e){
			e.preventDefault();
			THISPAGE.reset();
		});
		$("#btn_shop").click(function(){
			Utils.doQueryShop();
		});
	}
}
THISPAGE.init();